<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRulebtn"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <!-- <template slot-scope="scope"> -->
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="danger(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="end" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageparams.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pageparams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit?'编辑弹层':'增加弹层'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hclose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, delRole, addRoles, editrole } from '@/api/settings'
export default {
  data() {
    return {
      isEdit: false,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      total: 0,
      roleList: [],
      pageparams: {
        page: 1,
        pagesize: 2
      }
    }
  },
  computed: {
    getpagenum() {
      return Math.ceil((this.total + 1) / this.pageparams.pagesize)
      // return Math.ceil(this.total / this.pageparams.pagesize)
    }
    /*  getyushu() {
      return this.total % this.pageparams.pagesize === 0
    } */
  },
  created() {
    this.loadRules()
  },
  methods: {
    hclose() {
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },

    editRole(row) {
      this.showDialog = true
      this.isEdit = true
      this.roleForm = { ...row }
    },
    async editRolesub() {
      const res = await editrole(this.roleForm)
      console.log(res)
      this.loadRules()
      this.showDialog = false
    },
    addRulebtn() {
      this.showDialog = true
      this.isEdit = false
    },
    async  addRule() {
      const res = await addRoles(this.roleForm)
      this.$message.success(res.message)
      this.showDialog = false
      this.isEdit = false
      this.pageparams.page = this.getpagenum
      /*   this.pageparams.page = this.getpagenum
      if (this.getyushu) {
        this.pageparams.page++
        this.total++
      } */
      this.loadRules()
    },
    hSubmit() {
      this.isEdit ? this.editRolesub() : this.addRule()
    },
    danger(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delRole(id)
        if (this.roleList.length === 1) {
          this.pageparams.page--
          if (this.roleList.page <= 1) {
            this.pageparams.page = 1
          }
        }

        console.log(res)
        this.loadRules()
      }).catch(() => {

      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageparams.pagesize = val
      this.pageparams.page = 1
      this.loadRules()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pageparams.page = val
      this.loadRules()
    },
    async loadRules() {
      const res = await getRoles(this.pageparams)
      console.log(res)
      this.total = res.data.total
      this.roleList = res.data.rows
    }
  }
}
</script>
